<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.6.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="Java基础1.JAVA反射机制:java的反射机制是java中的一个重要特效。java编译期把java代码编译成class文件；运行期把编译后的文件交给计算机执行； java反射机制就是：在运行状态时，对于任意一个类，都能知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意方法和属性；这种动态获取信息以及动态调用对象方法的功能成为java的反射机制；简单来说，反射机制指的是程序在运">
<meta property="og:type" content="article">
<meta property="og:title" content="Java基础">
<meta property="og:url" content="http://example.com/2021/07/24/Java/index.html">
<meta property="og:site_name" content="风轻云淡">
<meta property="og:description" content="Java基础1.JAVA反射机制:java的反射机制是java中的一个重要特效。java编译期把java代码编译成class文件；运行期把编译后的文件交给计算机执行； java反射机制就是：在运行状态时，对于任意一个类，都能知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意方法和属性；这种动态获取信息以及动态调用对象方法的功能成为java的反射机制；简单来说，反射机制指的是程序在运">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img0.baidu.com/it/u=1844305177,3689457290&fm=253&fmt=auto&app=120&f=GIF?w=300&h=300">
<meta property="og:image" content="https://gitee.com/wqqszz/blogImg/raw/master/img/1647780199(1).jpg">
<meta property="article:published_time" content="2021-07-24T06:04:44.000Z">
<meta property="article:modified_time" content="2022-04-25T13:47:46.397Z">
<meta property="article:author" content="WQQ">
<meta property="article:tag" content="风">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img0.baidu.com/it/u=1844305177,3689457290&fm=253&fmt=auto&app=120&f=GIF?w=300&h=300">


<link rel="canonical" href="http://example.com/2021/07/24/Java/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/07/24/Java/","path":"2021/07/24/Java/","title":"Java基础"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java基础 | 风轻云淡</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
	<!-- 页面点击小红心 -->
	<script type="text/javascript" src="/js/src/clicklove.js"></script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">风轻云淡</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">清风吹拂，白云淡淡</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-首页"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-关于"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-标签"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-分类"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Java%E5%9F%BA%E7%A1%80"><span class="nav-number">1.</span> <span class="nav-text">Java基础</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-JAVA%E5%8F%8D%E5%B0%84%E6%9C%BA%E5%88%B6"><span class="nav-number">1.0.1.</span> <span class="nav-text">1.JAVA反射机制:</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E6%9C%89%E5%93%AA%E4%BA%9B%E3%80%82"><span class="nav-number">1.0.2.</span> <span class="nav-text">2.线程的实现方式有哪些。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-%E7%BA%BF%E7%A8%8B%E6%AD%BB%E9%94%81%E7%9A%84%E6%83%85%E5%86%B5%EF%BC%9F%E5%A6%82%E4%BD%95%E9%98%B2%E6%AD%A2%E7%BA%BF%E7%A8%8B%E6%AD%BB%E9%94%81%EF%BC%9F"><span class="nav-number">1.0.3.</span> <span class="nav-text">3.线程死锁的情况？如何防止线程死锁？</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-%E7%BA%BF%E7%A8%8B%E6%B1%A0"><span class="nav-number">1.0.4.</span> <span class="nav-text">4.线程池</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-try-catch-%E8%AF%AD%E5%8F%A5%E6%94%BE%E5%88%B0for%E5%BE%AA%E7%8E%AF%E5%86%85%E4%B8%8E%E6%94%BE%E5%88%B0for%E5%BE%AA%E7%8E%AF%E5%A4%96%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">1.0.5.</span> <span class="nav-text">5.try catch 语句放到for循环内与放到for循环外的区别</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-%E4%BA%8B%E5%8A%A1"><span class="nav-number">1.0.6.</span> <span class="nav-text">6.事务</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%BA%8B%E5%8A%A1%E5%88%86%E4%B8%BA%EF%BC%9A"><span class="nav-number">1.0.6.1.</span> <span class="nav-text">事务分为：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Transactional%EF%BC%9A"><span class="nav-number">1.0.6.2.</span> <span class="nav-text">@Transactional：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Transactional-%E5%A4%B1%E6%95%88%E5%9C%BA%E6%99%AF%EF%BC%9A"><span class="nav-number">1.0.6.3.</span> <span class="nav-text">@Transactional 失效场景：</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#7-%E9%9B%86%E5%90%88%E7%B1%BB%E5%8F%91%E5%B1%95%E5%8F%B2%EF%BC%9F"><span class="nav-number">1.0.7.</span> <span class="nav-text">7,集合类发展史？</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%AC%AC%E4%B8%80%E4%BB%A3%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E9%9B%86%E5%90%88%E7%B1%BB%EF%BC%9A"><span class="nav-number">1.0.7.1.</span> <span class="nav-text">第一代线程安全集合类：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%AC%AC%E4%BA%8C%E4%BB%A3%E7%BA%BF%E7%A8%8B%E9%9D%9E%E5%AE%89%E5%85%A8%E9%9B%86%E5%90%88%E7%B1%BB%EF%BC%9A"><span class="nav-number">1.0.7.2.</span> <span class="nav-text">第二代线程非安全集合类：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%AC%AC%E4%B8%89%E4%BB%A3%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E9%9B%86%E5%90%88%E7%B1%BB%EF%BC%9A"><span class="nav-number">1.0.7.3.</span> <span class="nav-text">第三代线程安全集合类：</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#8-%E7%AE%80%E5%8D%95%E8%AF%B4%E4%B8%8BJDK8%E7%9A%84%E6%96%B0%E7%89%B9%E6%80%A7%EF%BC%9A"><span class="nav-number">1.0.8.</span> <span class="nav-text">8,简单说下JDK8的新特性：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#9-%E6%8A%BD%E8%B1%A1%E7%B1%BB%E4%B8%8E%E6%8E%A5%E5%8F%A3%E6%9C%89%E4%BB%80%E4%B9%88%E5%8C%BA%E5%88%AB%EF%BC%9A"><span class="nav-number">1.0.9.</span> <span class="nav-text">9,抽象类与接口有什么区别：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#10%EF%BC%8CJava%E7%9A%84%E4%BB%A3%E7%90%86%E6%9C%89%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E3%80%82"><span class="nav-number">1.0.10.</span> <span class="nav-text">10，Java的代理有几种实现方式。</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#Proxy%E4%BB%A3%E7%90%86%E6%98%AFJDK%E5%86%85%E7%BD%AE%E7%9A%84%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%EF%BC%9A"><span class="nav-number">1.0.10.1.</span> <span class="nav-text">Proxy代理是JDK内置的动态代理：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#CGLIB%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%EF%BC%9A"><span class="nav-number">1.0.10.2.</span> <span class="nav-text">CGLIB动态代理：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#JDK%E5%8A%A8%E6%80%81%E4%BB%A3%E7%90%86%E4%B8%8ECGLIB%E5%AD%97%E8%8A%82%E7%A0%81%E7%94%9F%E6%88%90%E7%9A%84%E5%8C%BA%E5%88%AB%EF%BC%9F"><span class="nav-number">1.0.10.3.</span> <span class="nav-text">JDK动态代理与CGLIB字节码生成的区别？</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#11%EF%BC%8CString-StringBuffer-StringBuilder%E5%8C%BA%E5%88%AB%EF%BC%9F"><span class="nav-number">1.0.11.</span> <span class="nav-text">11，String, StringBuffer ,StringBuilder区别？</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#12-%E9%94%81"><span class="nav-number">1.0.12.</span> <span class="nav-text">12,锁</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B9%90%E8%A7%82%E9%94%81%E5%92%8C%E6%82%B2%E8%A7%82%E9%94%81"><span class="nav-number">1.0.12.1.</span> <span class="nav-text">乐观锁和悲观锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E7%8B%AC%E5%8D%A0%E9%94%81%E5%92%8C%E5%85%B1%E4%BA%AB%E9%94%81"><span class="nav-number">1.0.12.2.</span> <span class="nav-text">独占锁和共享锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%BA%92%E6%96%A5%E9%94%81%E5%92%8C%E8%AF%BB%E5%86%99%E9%94%81"><span class="nav-number">1.0.12.3.</span> <span class="nav-text">互斥锁和读写锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%85%AC%E5%B9%B3%E9%94%81%E5%92%8C%E9%9D%9E%E5%85%AC%E5%B9%B3%E9%94%81"><span class="nav-number">1.0.12.4.</span> <span class="nav-text">公平锁和非公平锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%8F%AF%E9%87%8D%E5%85%A5%E9%94%81"><span class="nav-number">1.0.12.5.</span> <span class="nav-text">可重入锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E8%87%AA%E6%97%8B%E9%94%81"><span class="nav-number">1.0.12.6.</span> <span class="nav-text">自旋锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%88%86%E6%AE%B5%E9%94%81"><span class="nav-number">1.0.12.7.</span> <span class="nav-text">分段锁</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E9%94%81%E5%8D%87%E7%BA%A7%EF%BC%88%E6%97%A0%E9%94%81-%E5%81%8F%E5%90%91%E9%94%81-%E8%BD%BB%E9%87%8F%E7%BA%A7%E9%94%81-%E9%87%8D%E9%87%8F%E7%BA%A7%E9%94%81%EF%BC%89"><span class="nav-number">1.0.12.8.</span> <span class="nav-text">锁升级（无锁 | 偏向锁 | 轻量级锁 | 重量级锁）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E9%94%81%E4%BC%98%E5%8C%96%E6%8A%80%E6%9C%AF"><span class="nav-number">1.0.12.9.</span> <span class="nav-text">锁优化技术</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#13%EF%BC%8C%E7%AE%80%E8%BF%B0%E4%B8%80%E4%B8%8BCAS"><span class="nav-number">1.0.13.</span> <span class="nav-text">13，简述一下CAS</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="WQQ"
      src="/images/aaa.jpg">
  <p class="site-author-name" itemprop="name">WQQ</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://gitee.com/wqqszz" title="GitHub → https:&#x2F;&#x2F;gitee.com&#x2F;wqqszz" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="QQ:1640611853@qq.com" title="E-Mail → QQ:1640611853@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



          </div>
        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/24/Java/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/aaa.jpg">
      <meta itemprop="name" content="WQQ">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="风轻云淡">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java基础
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-24 14:04:44" itemprop="dateCreated datePublished" datetime="2021-07-24T14:04:44+08:00">2021-07-24</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-04-25 21:47:46" itemprop="dateModified" datetime="2022-04-25T21:47:46+08:00">2022-04-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p><img src="https://img0.baidu.com/it/u=1844305177,3689457290&fm=253&fmt=auto&app=120&f=GIF?w=300&h=300" alt="示例图片"></p>
<h2 id="Java基础"><a href="#Java基础" class="headerlink" title="Java基础"></a>Java基础</h2><h4 id="1-JAVA反射机制"><a href="#1-JAVA反射机制" class="headerlink" title="1.JAVA反射机制:"></a>1.JAVA反射机制:</h4><p>java的反射机制是java中的一个重要特效。java编译期把java代码编译成class文件；运行期把编译后的文件交给计算机执行；</p>
<p>java反射机制就是：在运行状态时，对于任意一个类，都能知道这个类的所有属性和方法；对于任意一个对象，都能够调用它的任意方法和属性；<strong>这种动态获取信息以及动态调用对象方法的功能成为java的反射机制</strong>；简单来说，反射机制指的是程序在运行时能够获取自身的信息。在 Java 中，只要给定类的名字，就可以通过反射机制来获得类的所有信息。</p>
<p>Java 反射机制在服务器程序和中间件程序中得到了广泛运用。在服务器端，往往需要根据客户的请求，动态调用某一个对象的特定方法。此外，<strong>在 ORM 中间件的实现中，运用 Java 反射机制可以读取任意一个 JavaBean 的所有属性，或者给这些属性赋值。</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//通过new创建对象</span></span><br><span class="line">UserAccount userAccount=<span class="keyword">new</span> UserAccount();</span><br><span class="line">System.out.println(userAccount);</span><br><span class="line">System.out.println(userAccount);</span><br><span class="line"></span><br><span class="line"><span class="comment">//通过反射创建对象</span></span><br><span class="line">Class clas = Class.forName(<span class="string">&quot;com.atguigu.yygh.model.user.UserAccount&quot;</span>);</span><br><span class="line">Object obj=clas.getInstance();</span><br><span class="line">System.out.println(obj);</span><br><span class="line"><span class="comment">//打印结果如下</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* UserAccount(openId=null, nickName=null, loginTime=null, sessionKey=null, password=null)</span></span><br><span class="line"><span class="comment">/</span></span><br></pre></td></tr></table></figure>

<span id="more"></span>

<h4 id="2-线程的实现方式有哪些。"><a href="#2-线程的实现方式有哪些。" class="headerlink" title="2.线程的实现方式有哪些。"></a>2.线程的实现方式有哪些。</h4><pre><code>    ①，继承Thread类型， 重写run方法，无返回值， start方法启动线程
    ②，实现Runnable接口，重写run方法，无返回值，start方法启动线程
    ③，实现Callable接口，重写call方法，有返回值，start启动线程
    ④，通过线程池创建线程，通过Executors提供的线程池有：
            创建线程池有缓存线程池（newCachedThreadPool），
            定长线程池（newFixedThreadPool），
            定长线程池,支持定时和周期性任务执行（newScheduledThreadPool），
            单线程化的线程池（newSingleThreadExecutor）
        线程池的组成元素：
</code></pre>
<p>​                </p>
<h4 id="3-线程死锁的情况？如何防止线程死锁？"><a href="#3-线程死锁的情况？如何防止线程死锁？" class="headerlink" title="3.线程死锁的情况？如何防止线程死锁？"></a>3.线程死锁的情况？如何防止线程死锁？</h4><h4 id="4-线程池"><a href="#4-线程池" class="headerlink" title="4.线程池"></a>4.线程池</h4><h4 id="5-try-catch-语句放到for循环内与放到for循环外的区别"><a href="#5-try-catch-语句放到for循环内与放到for循环外的区别" class="headerlink" title="5.try catch 语句放到for循环内与放到for循环外的区别"></a>5.try catch 语句放到for循环内与放到for循环外的区别</h4><pre><code>​```
 实例： 
    public class Demo &#123;
        public static void main(String[] args) &#123;
            new Demo().testOne();
            System.out.println(&quot;-------------------&quot;);
            new Demo().testTwo();
        &#125;

        public void testOne()&#123;
            for(int i=0;i&lt;6;i++)&#123;
                try&#123;
                    int x;
                    if(i==3)&#123;
                        x=1/0;
                    &#125;else&#123;
                        x=i;
                        System.out.println(x);
                    &#125;
                &#125;catch (Exception e)&#123;
                    System.out.println(&quot;异常&quot;);
                &#125;
            &#125;
        &#125;
        public void testTwo()&#123;
            try&#123;
                for (int count=0;count&lt;6;count++)&#123;
                    int x;
                    if(count==3)&#123;
                        x=1/0;
                    &#125;else&#123;
                        x=count;
                        System.out.println(x);
                    &#125;
                &#125;
            &#125;catch (Exception e)&#123;
                System.out.println(&quot;异常2&quot;);
            &#125;
        &#125;
    &#125;
    
结果：try catch 放到for里面，如果有异常，后面的循环可以继续执行，
      try catch 放到for外面，如果有异常，则后面的循环都停止执行
    0
    1
    2
    异常
    4
    5
-------------------
    0
    1    
    2
    异常2
​```
</code></pre>
<h4 id="6-事务"><a href="#6-事务" class="headerlink" title="6.事务"></a>6.事务</h4><h5 id="事务分为："><a href="#事务分为：" class="headerlink" title="事务分为："></a>事务分为：</h5><p>​    <strong>编程式事务：</strong>是指在代码中手动的管理事务的提交，回滚操作，代码的侵入性较强，一般不建议使用，栗子如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">	transactionalManager.commit(status);</span><br><span class="line">&#125;<span class="keyword">catch</span>(Exception e)&#123;</span><br><span class="line">	transactionManager.rollback(status);</span><br><span class="line">	<span class="keyword">throw</span> <span class="keyword">new</span> InvoiceApplyException(<span class="string">&quot;异常失败&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​    <strong>声明式事务：</strong></p>
<p>基于AOP面向切面的，它将具体的业务与事务处理部分解耦，代码的侵入性很低，所有在实际开发中应用的比较多。</p>
<p>声明式事务有两种实现方式：一、是基于xml配置文件； 二、基于@Transactional 注解。</p>
<h5 id="Transactional："><a href="#Transactional：" class="headerlink" title="@Transactional："></a>@Transactional：</h5><p>​    <strong>@Transactional注解可以用在接口，类，方法上：</strong></p>
<ul>
<li><p>​    作用于类上：当把@Transactional 注解放在类上时，表示所有该类的public方法都配置相同的事务属性信息。</p>
</li>
<li><p>​    作用于方法上：必须作用在public方法上，当类配置了@Transactional ，方法上也配置了@Transactional，方法的事务会覆盖类的事务配置属性（就近原则）。</p>
</li>
<li><p>作用于接口：推荐这种使用方法，因为一旦标注在Interface上并且配置了Spring AOP 使用CGLib动态代理，将会导致@Transactional注解失效。</p>
<p>​**@Transactional注解属性：**</p>
</li>
</ul>
<p><strong>propagation属性：代表事务的传播行为，默认值是 Propagation.REQUIRED ；</strong></p>
<ul>
<li><p>​    Propagation.REQUIRED  ：如果当前存在事务，则加入该事务，如果当前不存在事务，则创建一个新的事务。（栗子：如果A方法和B方法都添加了注解，在默认传播模式下，A方法内部调用B方法，会把两个方法的事务合并成一个事务）（有就用，没有就创建新的用）。</p>
<ul>
<li><p>​Propagation.SUPPORTS: 如果当前存在事务，则加入该事务，如果当前不存在事务，则以非事务的方式继续运行（有就用，没有就不用）。</p>
</li>
<li><p>​Propagation.MANDATORY: 如果当前存在事务就加入事务，如果当前不存在事务就抛异常。</p>
</li>
</ul>
</li>
<li><p>​    Propagation.REQUIRES_NEW: 重新创建一个新的事务，如果当前事务存在，暂停当前事务。</p>
</li>
<li><p>​    Propagation.NOT_SUPPORTED: 以非事务方式运行，如果当前有事务，则暂停事务。</p>
</li>
<li><p>​    Propagation.NEVER: 以非事务方式运行，如果当前有事务，则抛出异常。</p>
</li>
<li><p>​    Propagation.NESTED: 和 Propagation.REQUIRED 效果一样。 </p>
</li>
</ul>
<p><strong>isolation 属性：事务隔离级别，默认值是Isolation.DEFAULT</strong></p>
<ul>
<li>Isolation.DEFAULT：使用底层数据库默认的隔离级别。</li>
<li>Isolation.READ_UNCOMMITTED</li>
<li>Isolation.READ_COMMITTED</li>
<li>Isolation.REPEATABLE_READ</li>
<li>Isolation.SERIALIZABLE</li>
</ul>
<p><strong>timeout属性：</strong></p>
<p>​    事务超时时间，默认值为 -1（默认值为不超市） ，如果超过该时间限制但是事务还没完成，则自动回滚事务。</p>
<p><strong>readOnly事务：</strong></p>
<p>指定事务是否为只读，默认值为false, 为了忽略那些不需要事务的方法，比如读取数据，可以设置 read-only 为 true。</p>
<p><strong>rollbackFor属性：</strong></p>
<p>栗子：@Transactional(rollbackFor=Exception.class)</p>
<p>用于指定能够触发事务回滚的异常类型，可以指定多个异常类型；</p>
<p><strong>noRollbackFor属性：</strong></p>
<p>抛出指定的异常类型，不回滚事务，也可以指定多个异常类型；    </p>
<h5 id="Transactional-失效场景："><a href="#Transactional-失效场景：" class="headerlink" title="@Transactional 失效场景："></a>@Transactional 失效场景：</h5><ol>
<li><p>@Transactional 作用在非 public 修饰的方法上；</p>
<p>如果@Transactional 注解应用在非public修饰的方法上时，事务会失效。</p>
</li>
<li><p>@Transactional 注解 属性propagation 设置错误；</p>
</li>
</ol>
<ol start="3">
<li>@Transactional 注解，设置属性 rollbackFor 设置错误</li>
</ol>
<ol start="4">
<li><p>同一个类中方法调用，导致 @Transactional 失效；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 此时同一个类中方法调用会导致事务失效</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> ResponseInfo  <span class="title">testOne</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">​	Test test=<span class="keyword">new</span> Test();</span><br><span class="line"></span><br><span class="line">​    <span class="keyword">this</span>.testTwo(test)</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Transactional(rollbackFor=Exception.class)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testTwo</span><span class="params">(Test test)</span></span>&#123;</span><br><span class="line"></span><br><span class="line">​	xxxxMapper.insert(test);</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<ol start="5">
<li>异常被catch 吃掉了，导致@Transactional 失效； </li>
</ol>
   <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 异常被catch吃掉了，导致事务失败</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="meta">@Transactional(rollbackFor=Exception.class)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span>  ResponseInfo <span class="title">testOne</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">​		<span class="keyword">try</span>&#123;</span><br><span class="line"></span><br><span class="line">​			<span class="keyword">int</span> x=<span class="number">1</span>/<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">​		&#125;<span class="keyword">catch</span>(Exception e)&#123;</span><br><span class="line"></span><br><span class="line">​			logger.info(e.getMessage());</span><br><span class="line"></span><br><span class="line">​		&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">*  异常被catch后，又进行抛出，此时事务生效的</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="meta">@Transactional(rollbackFor=Exception.class)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span>  ResponseInfo <span class="title">testOne</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">​		<span class="keyword">try</span>&#123;</span><br><span class="line"></span><br><span class="line">​			<span class="keyword">int</span> x=<span class="number">1</span>/<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">​		&#125;<span class="keyword">catch</span>(Exception e)&#123;</span><br><span class="line"></span><br><span class="line">​			logger.info(e.getMessage());</span><br><span class="line">			<span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">&quot;xxxxxx&quot;</span>);</span><br><span class="line">​		&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="6">
<li><p>数据库引擎不支持事务，导致事务失效；</p>
<p>​</p>
<h4 id="7-集合类发展史？"><a href="#7-集合类发展史？" class="headerlink" title="7,集合类发展史？"></a>7,集合类发展史？</h4><h5 id="第一代线程安全集合类："><a href="#第一代线程安全集合类：" class="headerlink" title="第一代线程安全集合类："></a>第一代线程安全集合类：</h5><p>​    Vector ,HashTable</p>
<p>​    是怎么保证线程安全的：使用synchronsized修饰方法。</p>
<p>​    缺点：效率低下。</p>
<h5 id="第二代线程非安全集合类："><a href="#第二代线程非安全集合类：" class="headerlink" title="第二代线程非安全集合类："></a>第二代线程非安全集合类：</h5><p>​    ArrayList,LinkedList, HashMap</p>
<p>​    线程不安全，但是性能好，用来替代Vector,  Hashtable </p>
<p>​    使用ArrayList, HashMap ,需要线程安全怎么办？</p>
<p>​    使用Collections.synchronizedList(list); Collections.synchronizedMap(map);</p>
<p>​    底层使用synchronized 代码块锁。</p>
<h5 id="第三代线程安全集合类："><a href="#第三代线程安全集合类：" class="headerlink" title="第三代线程安全集合类："></a>第三代线程安全集合类：</h5><p>​    在大量并发情况下如何提高集合的效率和安全呢？</p>
<p>​    java.util.concurrent.*</p>
<p>​    ConcurrentHashMap;</p>
<p>​    CopyOnWriteArrayList;</p>
<p>​    CopyOnWriteArraySet;</p>
<p>​    底层大都采用的Lock锁，保证安全同时，性能也很高。</p>
<p>​    （jdk8的ConcurrentHashMap不使用Lock锁）</p>
</li>
</ol>
<h4 id="8-简单说下JDK8的新特性："><a href="#8-简单说下JDK8的新特性：" class="headerlink" title="8,简单说下JDK8的新特性："></a>8,简单说下JDK8的新特性：</h4><h4 id="9-抽象类与接口有什么区别："><a href="#9-抽象类与接口有什么区别：" class="headerlink" title="9,抽象类与接口有什么区别："></a>9,抽象类与接口有什么区别：</h4><p>​    <strong>抽象类：</strong>    </p>
<ol>
<li>​        抽象类中可以定义构造器。</li>
<li>​    可以有抽象方法和具体方法。    </li>
<li>​抽象类中的成员变量可以是private, 默认，protected ,public</li>
<li>​有抽象方法的类必须被声明为抽象类，而抽象类未必有抽象方法。</li>
<li>​抽象类中可以包含静态方法。</li>
<li>​一个类只能继承一个抽象类。（一个子类只能有一个父类，一个父类可以有多个子类）</li>
</ol>
<p>   ​<strong>接口：</strong></p>
<pre><code>1. ​接口中不能定义构造器。

 2. ​方法全部都是抽象方法。        

2. ​接口中的成员全都是public。

3. ​接口中定义的成员变量实际上都是常量。

4. ​接口中不能有静态方法。

5. ​一个类可以实现多个接口。
</code></pre>
<p>  ​</p>
<p>  <strong>什么时候定义抽象类，什么时候定义接口？</strong></p>
<p>  ​    当描述特征的时候用接口。（比如，“飞”，会飞的可以有鸟，飞机.,,,,,,）</p>
<p>  ​    当描述概念的时候用抽象类。（比如宠物，狗，猫）</p>
<h4 id="10，Java的代理有几种实现方式。"><a href="#10，Java的代理有几种实现方式。" class="headerlink" title="10，Java的代理有几种实现方式。"></a>10，Java的代理有几种实现方式。</h4><p>​    第一种：静态代理，只能静态的代理某些类或者某些方法。</p>
<p>​    第二种：动态代理，动态代理包含JDK内置的动态代理（Proxy代理），CGLIB动态代理</p>
<h5 id="Proxy代理是JDK内置的动态代理："><a href="#Proxy代理是JDK内置的动态代理：" class="headerlink" title="Proxy代理是JDK内置的动态代理："></a>Proxy代理是JDK内置的动态代理：</h5><p>​        java（JDK）动态代理是利用反射机制生成一个实现代理接口的匿名类，在调用具体方法调用前调用InvokeHandler来处理。</p>
<h5 id="CGLIB动态代理："><a href="#CGLIB动态代理：" class="headerlink" title="CGLIB动态代理："></a>CGLIB动态代理：</h5><p>​        cglib动态代理利用asm，对代理对象类的class文件加载进来，通过修改字节码生成子类来处理。</p>
<p>栗子：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lf.shejimoshi.proxy.entity;</span><br><span class="line"><span class="comment">//用户管理接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserManager</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">//新增用户抽象方法</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">addUser</span><span class="params">(String userName,String password)</span></span>;</span><br><span class="line"><span class="comment">//删除用户抽象方法</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">delUser</span><span class="params">(String userName)</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lf.shejimoshi.proxy.entity;</span><br><span class="line"><span class="comment">//用户管理实现类,实现用户管理接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserManagerImpl</span> <span class="keyword">implements</span> <span class="title">UserManager</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//重写新增用户方法</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addUser</span><span class="params">(String userName, String password)</span> </span>&#123;</span><br><span class="line">    	System.out.println(<span class="string">&quot;调用了新增的方法！&quot;</span>);</span><br><span class="line">   	 	System.out.println(<span class="string">&quot;传入参数为 userName: &quot;</span>+userName+<span class="string">&quot; password: &quot;</span>+password);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//重写删除用户方法</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delUser</span><span class="params">(String userName)</span> </span>&#123;</span><br><span class="line">    	System.out.println(<span class="string">&quot;调用了删除的方法！&quot;</span>);</span><br><span class="line">      System.out.println(<span class="string">&quot;传入参数为 userName: &quot;</span>+userName);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lf.shejimoshi.proxy.jdk;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationHandler;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Proxy;</span><br><span class="line"><span class="keyword">import</span> com.lf.shejimoshi.proxy.entity.UserManager;</span><br><span class="line"><span class="keyword">import</span> com.lf.shejimoshi.proxy.entity.UserManagerImpl;</span><br><span class="line"></span><br><span class="line"><span class="comment">//JDK动态代理实现InvocationHandler接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JdkProxy</span> <span class="keyword">implements</span> <span class="title">InvocationHandler</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> Object target ;<span class="comment">//需要代理的目标对象</span></span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(Object proxy, Method method, Object[] args)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">    	System.out.println(<span class="string">&quot;JDK动态代理，监听开始！&quot;</span>);</span><br><span class="line">    	Object result = method.invoke(target, args);</span><br><span class="line">    	System.out.println(<span class="string">&quot;JDK动态代理，监听结束！&quot;</span>);</span><br><span class="line">    	<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//定义获取代理对象方法</span></span><br><span class="line">	<span class="function"><span class="keyword">private</span> Object <span class="title">getJDKProxy</span><span class="params">(Object targetObject)</span></span>&#123;</span><br><span class="line">    	<span class="comment">//为目标对象target赋值</span></span><br><span class="line">   	 	<span class="keyword">this</span>.target = targetObject;</span><br><span class="line">    	<span class="comment">//JDK动态代理只能针对实现了接口的类进行代理，newProxyInstance 函数所需参数就可看出</span></span><br><span class="line">    	<span class="keyword">return</span> Proxy.newProxyInstance(targetObject.getClass().getClassLoader(), 		  targetObject.getClass().getInterfaces(), <span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    	JdkProxy jdkProxy = <span class="keyword">new</span> JdkProxy();<span class="comment">//实例化JDKProxy对象</span></span><br><span class="line">    	UserManager user = (UserManager) jdkProxy.getJDKProxy(<span class="keyword">new</span> UserManagerImpl());<span class="comment">//获取代理对象</span></span><br><span class="line">    	user.addUser(<span class="string">&quot;admin&quot;</span>, <span class="string">&quot;123123&quot;</span>);<span class="comment">//执行新增方法</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.lf.shejimoshi.proxy.cglib;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"><span class="keyword">import</span> com.lf.shejimoshi.proxy.entity.UserManager;</span><br><span class="line"><span class="keyword">import</span> com.lf.shejimoshi.proxy.entity.UserManagerImpl;</span><br><span class="line"><span class="keyword">import</span> net.sf.cglib.proxy.Enhancer;</span><br><span class="line"><span class="keyword">import</span> net.sf.cglib.proxy.MethodInterceptor;</span><br><span class="line"><span class="keyword">import</span> net.sf.cglib.proxy.MethodProxy;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//Cglib动态代理，实现MethodInterceptor接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CglibProxy</span> <span class="keyword">implements</span> <span class="title">MethodInterceptor</span> </span>&#123;</span><br><span class="line">  </span><br><span class="line">	<span class="keyword">private</span> Object target;<span class="comment">//需要代理的目标对象</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//重写拦截方法</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">intercept</span><span class="params">(Object obj, Method method, Object[] arr, MethodProxy proxy)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">    	System.out.println(<span class="string">&quot;Cglib动态代理，监听开始！&quot;</span>);</span><br><span class="line">    	Object invoke = method.invoke(target, arr);<span class="comment">//方法执行，参数：target 目标对象 arr参数数组</span></span><br><span class="line">    	System.out.println(<span class="string">&quot;Cglib动态代理，监听结束！&quot;</span>);</span><br><span class="line">    	<span class="keyword">return</span> invoke;</span><br><span class="line">	&#125;</span><br><span class="line">  </span><br><span class="line">	<span class="comment">//定义获取代理对象方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">getCglibProxy</span><span class="params">(Object objectTarget)</span></span>&#123;</span><br><span class="line">   	 	<span class="comment">//为目标对象target赋值</span></span><br><span class="line">    	<span class="keyword">this</span>.target = objectTarget;</span><br><span class="line">    	Enhancer enhancer = <span class="keyword">new</span> Enhancer();</span><br><span class="line">    	<span class="comment">//设置父类,因为Cglib是针对指定的类生成一个子类，所以需要指定父类</span></span><br><span class="line">    	enhancer.setSuperclass(objectTarget.getClass());</span><br><span class="line">    	enhancer.setCallback(<span class="keyword">this</span>);<span class="comment">// 设置回调 </span></span><br><span class="line">    	Object result = enhancer.create();<span class="comment">//创建并返回代理对象</span></span><br><span class="line">    	<span class="keyword">return</span> result;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    	CglibProxy cglib = <span class="keyword">new</span> CglibProxy();<span class="comment">//实例化CglibProxy对象</span></span><br><span class="line">    	UserManager user =  (UserManager) cglib.getCglibProxy(<span class="keyword">new</span> UserManagerImpl());<span class="comment">//获取代理对象</span></span><br><span class="line">    	user.delUser(<span class="string">&quot;admin&quot;</span>);<span class="comment">//执行删除方法</span></span><br><span class="line">  	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h5 id="JDK动态代理与CGLIB字节码生成的区别？"><a href="#JDK动态代理与CGLIB字节码生成的区别？" class="headerlink" title="JDK动态代理与CGLIB字节码生成的区别？"></a>JDK动态代理与CGLIB字节码生成的区别？</h5><ol>
<li>JDK动态代理只能对实现了接口的类生成代理，而不能针对类。</li>
<li>CGLIB是针对类实现代理，主要是对指定的类生成子类，覆盖其中的方法。        </li>
</ol>
<h4 id="11，String-StringBuffer-StringBuilder区别？"><a href="#11，String-StringBuffer-StringBuilder区别？" class="headerlink" title="11，String, StringBuffer ,StringBuilder区别？"></a>11，String, StringBuffer ,StringBuilder区别？</h4><ul>
<li>String  长度不可变，是常量，    被final修饰，value是一个final修饰的char数组，final修饰的。这就意味着value在构造器中被初始化后，无法再被修改了。</li>
<li>StringBuilder  可变字符串，线程非安全，在不是高并发情况下效率相比StringBuffer快。</li>
<li>StringBuffer  可变字符串，线程安全，方法都是由synchronsized代码块。</li>
</ul>
<p>​ </p>
<h4 id="12-锁"><a href="#12-锁" class="headerlink" title="12,锁"></a>12,锁</h4><h5 id="乐观锁和悲观锁"><a href="#乐观锁和悲观锁" class="headerlink" title="乐观锁和悲观锁"></a>乐观锁和悲观锁</h5><p>​    悲观锁：一个共享数据如果添加了悲观锁，那线程每次操作这个数据的时候都会认为有其他线程也可能会操作这个数据，所以每次操作前都会上锁，这样其他线程想操作这个数据就拿不到锁，其他线程就阻塞状态。</p>
<p><img src="https://gitee.com/wqqszz/blogImg/raw/master/img/1647780199(1).jpg"></p>
<p>​    栗子： synchronized 和 ReetrantLock 就是典型的悲观锁，还有使用了 synchronized 关键字的容器类如 HashTable 等也是悲观锁。（HashTable线程安全，用synchronized 封装的代码块，联想：HashMap , HashTable区别）。</p>
<p>​    乐观锁：乐观锁操作数据时不会上锁，在更新的时候会判断一下在此期间是否有其他线程去更新这个数据。每次获取数据的时候，都不会担心数据被修改，所以每次获取数据的时候都不会进行加锁，但是在更新数据的时候需要判断该数据是否被别人修改过。如果数据被其他线程修改，则不进行数据更新，如果数据没有被其他线程修改，则进行数据更新。由于数据没有进行加锁，期间该数据可以被其他线程进行读写操作。</p>
<p>​    栗子：乐观锁可以使用版本号 和CAS 算法实现。在 Java中，java.util.concurrent.atomic 包下的原子类就是 使用CAS乐观锁实现的。</p>
<p>乐观锁和悲观锁区别：</p>
<ul>
<li><p>​    乐观锁适用于<strong>写比较少</strong>的场景，因为不用上锁，释放锁，所以省去了锁的开销，提高了吞吐量。</p>
<ul>
<li>悲观锁适用于 <strong>写多读少</strong>的场景，减少了线程间的竞争。    </li>
</ul>
<p>​</p>
<h5 id="独占锁和共享锁"><a href="#独占锁和共享锁" class="headerlink" title="独占锁和共享锁"></a>独占锁和共享锁</h5><p>独占锁：是指锁一次只能被一个线程所持有，如果一个线程对数据加上了排它锁后，那么其他线程不能再对改数据加上任何类型的锁，获得独占锁的线程既能读数据又能修改数据。</p>
<p>​栗子：JDK中的 synchronized 和 java.util.concurrent 包中 Lock 的实现类就是独占锁。</p>
<p>共享锁：是指锁可以被多个线程所持有，如果一个线程对数据加上了共享锁后，那么其他线程只能对数据再加共享锁，不能加独占锁，获得共享锁的线程只能读数据，不能改数据。</p>
<p>​栗子：JDK中的ReentrantReadWriteLock 就是共享锁。    </p>
<h5 id="互斥锁和读写锁"><a href="#互斥锁和读写锁" class="headerlink" title="互斥锁和读写锁"></a>互斥锁和读写锁</h5><p>​    互斥锁：是独占锁的一种常规实现，是指某一资源同时只允许一个访问者对其进行访问，其具有唯一性和排他性。互斥锁一次只能一个线程拥有互斥锁，其他线程只有等待。</p>
<p>​    读写锁：读写锁是共享锁的一种具体实现，读写锁管理一组锁，一个是只读的锁，一个是写锁，读锁可以在没有写锁的时候被多个线程同时持有，而写锁是独占的，写锁的优先级要高于读锁，一个获得了读锁的线程必须能看到前一个释放的写锁所更新的内容。</p>
<p>​    读写锁相比于互斥锁并发程度高，每次只能有一个写线程，但是同时可以有多个线程并发读。</p>
<p>​    栗子：jdk中读写锁的接口：ReadWriteLock , ReetrantReadWriteLock</p>
<p>​            </p>
<h5 id="公平锁和非公平锁"><a href="#公平锁和非公平锁" class="headerlink" title="公平锁和非公平锁"></a>公平锁和非公平锁</h5><p>​    公平锁：是指多个线程按照申请的顺序来获取锁，先申请的先获取锁，后申请的后获取锁（先到先得原则）。</p>
<p>​    java中通过构造函数初始化公平锁</p>
<p>​    </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">*创建一个可重入锁，true表示公平锁，false非公平锁，默认非公平锁</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">Lock lock = <span class="keyword">new</span> ReentrantLock(<span class="keyword">true</span>);</span><br></pre></td></tr></table></figure>

<p>​    非公平锁：指多个线程获取锁的顺序并不是按照申请锁的顺序，有可能是先申请的线程比后申请的线程还后获取到锁，（不遵循先到先得原则）。</p>
<p>​        </p>
<p>java中，synchronized关键字是非公平锁。ReentrantLock默认也是非公平锁“</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Lock lock = <span class="keyword">new</span> ReentrantLock(<span class="keyword">false</span>);</span><br></pre></td></tr></table></figure>

<p>​</p>
<h5 id="可重入锁"><a href="#可重入锁" class="headerlink" title="可重入锁"></a>可重入锁</h5><h5 id="自旋锁"><a href="#自旋锁" class="headerlink" title="自旋锁"></a>自旋锁</h5><h5 id="分段锁"><a href="#分段锁" class="headerlink" title="分段锁"></a>分段锁</h5><h5 id="锁升级（无锁-偏向锁-轻量级锁-重量级锁）"><a href="#锁升级（无锁-偏向锁-轻量级锁-重量级锁）" class="headerlink" title="锁升级（无锁 | 偏向锁 | 轻量级锁 | 重量级锁）"></a>锁升级（无锁 | 偏向锁 | 轻量级锁 | 重量级锁）</h5><h5 id="锁优化技术"><a href="#锁优化技术" class="headerlink" title="锁优化技术"></a>锁优化技术</h5><p>​注意：引自  <a target="_blank" rel="noopener" href="https://blog.csdn.net/guoguo527/article/details/118004077">https://blog.csdn.net/guoguo527/article/details/118004077</a></p>
</li>
</ul>
<h4 id="13，简述一下CAS"><a href="#13，简述一下CAS" class="headerlink" title="13，简述一下CAS"></a>13，简述一下CAS</h4><p>​    基本概念：CAS (compareAndSwap) , 也叫 “比较交换”，是一种无锁原子算法（保证原子性）.</p>
<p>​    作用：是让 CPU将内存值更新为新值，    但是必须满足以下条件（内存值必须与期望值相等）</p>
<p>​    包含3个参数 CAS (V, E, N) , V表示<strong>待更新的内存值</strong>，E表示<strong>预期值</strong>，N表示<strong>新值</strong>，当V值等于E值时，才会将V值更新成N值。如果V值与E值不等，则不作更新，这就是CAS的一次操作。</p>
<p><strong>ABA问题：</strong></p>
<p>​    CAS检查待更新的内存值有没有被修改，如果没有则更新，但是存在这么一种情况：<strong>如果一个值原来是A ,突然变成了B，之后又变成了A , 此时，CAS检查时会发现待更新值与原来一样，待更新值没被修改，而实际是有变动的</strong>。这就导致了ABA问题。</p>
<p>​    如何解决ABA问题：追加版本号，每次改变时加1 可以解决该问题。在java中通过 AtomicStampedRdference 可以实现这个方案。AtomicReference 可以把CAS 使用在对象上。</p>
<p>83e5d94a28404f69265317bb087330cb</p>
<p>wqqszz   blogImg    img           </p>
<p><a target="_blank" rel="noopener" href="https://baijiahao.baidu.com/s?id=1700437620645314374&amp;wfr=spider&amp;for=pc">https://baijiahao.baidu.com/s?id=1700437620645314374&amp;wfr=spider&amp;for=pc</a></p>
<p>​    <a target="_blank" rel="noopener" href="https://pics0.baidu.com/feed/2cf5e0fe9925bc317fb58c4b684b04b9c9137091.jpeg?token=8898835e22e9604c8978915f97609739">https://pics0.baidu.com/feed/2cf5e0fe9925bc317fb58c4b684b04b9c9137091.jpeg?token=8898835e22e9604c8978915f97609739</a></p>

    </div>

    
    
    
	
	<div>
		
			<div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
		
	</div>

    <footer class="post-footer">

        

          <div class="post-nav">
            <div class="post-nav-item">
            </div>
            <div class="post-nav-item">
                <a href="/2021/07/24/rocketMq/" rel="next" title="中间件">
                  中间件 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">WQQ</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
